package pack1;

import java.io.IOException;
import java.util.List;

import javax.naming.InitialContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.swim.entity.RegisteredUser;
import com.swim.entity.Skill;
import com.swim.session.RegisteredUserRemote;
import com.swim.session.SkillRemote;

public class EditProfileServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
      
    
    public EditProfileServlet() {
        super();
    }
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Object auth = request.getSession().getAttribute("authenticated");
		if(auth != null && (boolean)auth ){
			InitialContext jndiContext;
			try {
				jndiContext = (InitialContext) Util.getInitialContext();
				Object ref = jndiContext.lookup("RegisteredUserBeanJNDI");
				RegisteredUserRemote r = (RegisteredUserRemote) ref;
				RegisteredUser user = r.getUserWithEmail((String)request.getSession().getAttribute("authenticated-email"));
				request.setAttribute("user", user);
				
				ref = jndiContext.lookup("SkillBeanJNDI");
				SkillRemote s = (SkillRemote) ref;
				
				List<Skill> skills = s.getSkills();
				if(skills.size()==0) {
					request.setAttribute("title","The website is not configured yet!");
					request.setAttribute("message","<p>You can't register yet, you have to wait for the administrator to configure the website.</p>");
					RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/target.jsp");
					dispatcher.forward(request, response); 
					return;
				}
				request.setAttribute("skills", skills);
				
				List<Skill> userSkills = s.getSkillByEmail(user.getEmail());
				request.setAttribute("userSkills", userSkills);
				RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/auth/edit.jsp");
				dispatcher.forward(request, response); 
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			response.sendRedirect("index.jsp");
		}
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Object auth = request.getSession().getAttribute("authenticated");
		if(auth != null && (boolean)auth ){
			try{		
			String password = request.getParameter("password");
			String name = request.getParameter("name");
			String surname = request.getParameter("surname");
			String city = request.getParameter("city");
			
			if(name == null || surname == null || city == null){
				response.sendRedirect("index.jsp");
			} else {
				
				response.setContentType("text/html");
				InitialContext jndiContext = (InitialContext) Util.getInitialContext();
				Object ref = jndiContext.lookup("RegisteredUserBeanJNDI");
				RegisteredUserRemote r = (RegisteredUserRemote) ref;

				if(password == null)
					password = "";
				
				boolean user = false;
				String[] skills = request.getParameterValues("checkboxes");
				
				if(skills!=null && skills.length != 0){
					user = r.editUser((String)request.getSession().getAttribute("authenticated-email"),password,name,surname,city);
					r.addSkill(skills,(String)request.getSession().getAttribute("authenticated-email"));
				} else
					throw new Exception();
				if(!user) {
					throw new Exception();
				}
				
				request.setAttribute("title","Profile edited");
				request.setAttribute("message","Click <a href=\"index.jsp\">here</a> to go back to the homepage.");
				RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/target.jsp");
				dispatcher.forward(request, response); 
			}
			} catch (Exception e) {
				request.setAttribute("title","Error");
				request.setAttribute("message","There was an error during the profile editing. Click <a href=\"index.jsp\">here</a> to go back to the homepage.");
				RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/target.jsp");
				dispatcher.forward(request, response); 
				return;
			}
		} else {
    		response.sendRedirect("index.jsp");
		}
	}
	
}
